import 'dart:convert';

import 'ac_login_model.dart';

T? asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class AcBloggerModel {
  AcBloggerModel({
    required this.name,
    required this.followers,
    this.userInfo,
    required this.post,
  });

  factory AcBloggerModel.fromJson(Map<String, dynamic> json) {
    final List<Post>? post = json['post'] is List ? <Post>[] : null;
    if (post != null) {
      for (final dynamic item in json['post']!) {
        if (item != null) {
          post.add(Post.fromJson(asT<Map<String, dynamic>>(item)!));
        }
      }
    }
    return AcBloggerModel(
      name: asT<String>(json['name'])!,
      followers: asT<int>(json['followers'])!,
      userInfo: json['userInfo'] == null
          ? null
          : Userinfo.fromJson(asT<Map<String, dynamic>>(json['userInfo'])!),
      post: post!,
    );
  }

  String name;
  int followers;
  Userinfo? userInfo;
  List<Post> post;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
    'name': name,
    'followers': followers,
    'userInfo': userInfo,
    'post': post,
  };
}

class Post {
  Post({
    required this.title,
    required this.content,
    required this.likes,
    required this.mainImage,
    required this.images,
    required this.isLiked,
    required this.comment,
  });

  factory Post.fromJson(Map<String, dynamic> json) {
    final List<String>? images = json['images'] is List ? <String>[] : null;
    if (images != null) {
      for (final dynamic item in json['images']!) {
        if (item != null) {
          images.add(asT<String>(item)!);
        }
      }
    }
    final List<String> comment = json['comment'] is List ? <String>[] : [];

    return Post(
      title: asT<String>(json['title'])!,
      content: asT<String>(json['content'])!,
      likes: asT<int>(json['likes'])!,
      mainImage: asT<String>(json['mainImage'])!,
      isLiked: asT<bool>(json['isLiked'])!,
      images: images!,
      comment: comment,
    );
  }

  String title;
  String content;
  int likes;
  bool isLiked;
  String mainImage;
  List<String> images;
  List<String> comment;

  @override
  String toString() {
    return jsonEncode(this);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
    'title': title,
    'content': content,
    'likes': likes,
    'isLiked': isLiked,
    'mainImage': mainImage,
    'images': images,
    'comment': comment,
  };
}
